package com.bluishoul.order;

import java.sql.Timestamp;
import java.util.Calendar;

import my.db.QueryHelper;

import com.bluishoul.bean.Pojo;

public class Order extends Pojo {

	public static Order INSTANCE = new Order();

	public final static byte ORDER_TYPE_SINGLE = 0x0;
	public final static byte ORDER_TYPE_GROUP = 0x1;
	private long id;
	private long user;
	private String ident;
	private Timestamp create_time;
	private Timestamp end_time;
	private long seconds;
	private byte type;
	private String ip;

	public boolean tooQuick() {
		String sql = "SELECT create_time FROM menu_order WHERE ip = ? ORDER BY create_time DESC LIMIT 1 ";
		Timestamp time = QueryHelper.read(Timestamp.class, sql, this.ip);
		if (time == null)
			return false;
		Calendar now1 = Calendar.getInstance();
		Calendar now2 = Calendar.getInstance();
		now1.setTime(time);
		now2.add(Calendar.MINUTE, -5);
		return now2.before(now1);
	}

	public Order getOrderByIdent(String ident, long user) {
		String sql = "";
		if (user == 0) {
			sql = "SELECT * FROM menu_order WHERE ident=? ORDER BY create_time DESC LIMIT 1";
			return QueryHelper.read(Order.class, sql, ident);
		} else {
			sql = "SELECT * FROM menu_order WHERE user=? ORDER BY create_time DESC LIMIT 1";
			return QueryHelper.read(Order.class, sql, user);
		}
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public Timestamp getCreate_time() {
		return create_time;
	}

	public void setCreate_time(Timestamp create_time) {
		this.create_time = create_time;
	}

	public Timestamp getEnd_time() {
		return end_time;
	}

	public void setEnd_time(Timestamp end_time) {
		this.end_time = end_time;
	}

	public long getSeconds() {
		return seconds;
	}

	public void setSeconds(long seconds) {
		this.seconds = seconds;
	}

	public byte getType() {
		return type;
	}

	public void setType(byte type) {
		this.type = type;
	}

	public long getUser() {
		return user;
	}

	public void setUser(long user) {
		this.user = user;
	}

	public String getIdent() {
		return ident;
	}

	public void setIdent(String ident) {
		this.ident = ident;
	}

	@Override
	protected String TableName() {
		return "menu_order";
	}

	public String getIp() {
		return ip;
	}

	public void setIp(String ip) {
		this.ip = ip;
	}

}
